package network.TCP.file;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/22 10:10
 * 文件传输客户端
 */
public class FileClient {
    public static void main(String[] args) throws Exception {
        //建立连接
        Socket socket = new Socket(InetAddress.getLocalHost(), 9001);
        OutputStream outputStream = socket.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream("a.jpg");
        int len;
        byte[] bytes = new byte[1024];
        while ((len=fileInputStream.read(bytes))!=-1){
            outputStream.write(bytes,0,len);
        }
        //通知服务端我传输完毕
        socket.shutdownOutput();

        //获得服务端返回的数据
        InputStream inputStream = socket.getInputStream();
        //创建缓冲流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int lens;
        byte[] bytess = new byte[1024];
        while ((len=inputStream.read(bytess))!=-1){
            byteArrayOutputStream.write(bytess,0,len);
        }
        System.out.println(byteArrayOutputStream);

        byteArrayOutputStream.close();
        inputStream.close();
        fileInputStream.close();
        outputStream.close();
        socket.close();
    }
}
